using System;
using System.IO;
using System.Text;
using System.Xml;

using LocalBoard.Domain;
using LocalBoard.Persistence;

using NUnit.Framework;

using Rss;

namespace LocalBoard.Controller
{
	/// <summary>
	/// Summary description for TestRssController.
	/// </summary>
	[TestFixture]
	public class TestRssController
	{
		private RssController _unitUnderTest = null;
		private MockAccessorFactory _accessorFactory = null;
		private int _testClubId = 0;

		[SetUp]
		public void SetUp()
		{
			_accessorFactory = new MockAccessorFactory();
			AdminController adminController = new AdminController(_accessorFactory);
			_testClubId = adminController.CreateClub(new Club("test", "test", 1, "test", ClubType.Public.ToString(), null));
			ClubController clubController = new ClubController(_testClubId, _accessorFactory);
			int board1Id = clubController.CreateBoard(new Board("testBoard1", null, 0, 0, _testClubId, BoardViewType.List));
			int board2Id = clubController.CreateBoard(new Board("testBoard2", null, 0, 0, _testClubId, BoardViewType.List));
			int user1Id = adminController.CreateUser(new Profile("test", "test", "test", "test@test.com"));
			int user2Id = adminController.CreateUser(new Profile("test2", "test2", "test2", "test2@test.com"));
			Profile user1 = adminController.GetUser(user1Id);
			Profile user2 = adminController.GetUser(user2Id);
			BoardController board1Controller = new BoardController(board1Id, clubController, 0, _accessorFactory);
			BoardController board2Controller = new BoardController(board2Id, clubController, 0, _accessorFactory);
			board1Controller.PostMessage("test message", "test message test message test message test message test message test message test message test message test message test message test message test message test message test message test message ", user1, 0);
			board1Controller.PostMessage("test message", "test message body", user2, 0);
			board2Controller.PostMessage("test message", "test body test body test body test body test body test body test body test body test message test message test message test message test message test message test message ", user2, 0);
			board2Controller.PostMessage("test message", "test2 message body", user1, 0);
		}

		[TearDown]
		public void TearDown()
		{
			_unitUnderTest = null;
		}

		[Test]
		public void testGetRssOutputClub()
		{
			Club club = new AdminController(_accessorFactory).GetClub(_testClubId);
			_unitUnderTest = new RssController("LocalBoard.Web" + " - " + club.Name, "http://localhost/LocalBoard.Web/ViewClub.aspx?club=" + club.Id.ToString(), club.Description, "en-US", "http://localhost/LocalBoard.Web/ViewBoard.aspx?club={0}&board={1}&message={2}", _accessorFactory);
			MemoryStream stream = new MemoryStream(1024);
			_unitUnderTest.ConstructRssFeed(club);
			_unitUnderTest.WriteRss(stream);
			RssFeed feed = RssFeed.Read(new MemoryStream(stream.ToArray()));
			Assert.AreEqual(4, feed.Channels[0].Items.Count);
		}

		[Test]
		public void testGetRssOutputBoard()
		{
			Club club = new AdminController(_accessorFactory).GetClub(_testClubId);
			Board board = new ClubController(_testClubId, _accessorFactory).GetBoard(1);

			_unitUnderTest = new RssController("LocalBoard.Web" + " - " + club.Name + " : " + board.Name, string.Format("http://localhost/LocalBoard.Web/ViewBoard.aspx?club={0}&board={1}", club.Id,  board.Id), board.Name, "en-US", "http://localhost/LocalBoard.Web/ViewBoard.aspx?club={0}&board={1}&message={2}", _accessorFactory);
			MemoryStream stream = new MemoryStream(1024);
			_unitUnderTest.ConstructRssFeed(board);
			_unitUnderTest.WriteRss(stream);
			RssFeed feed = RssFeed.Read(new MemoryStream(stream.ToArray()));
			Assert.AreEqual(2, feed.Channels[0].Items.Count);
		}
	}
}
